package com.greathiit.printserver.receiver;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.google.gson.Gson;
//https://www.jianshu.com/p/83861676051c
/* 
 		public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames("zhihao.miao.order");

        MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageHandler());
        //指定Json转换器
        Jackson2JsonMessageConverter jackson2JsonMessageConverter =new Jackson2JsonMessageConverter();

        //消费端配置映射
        Map<String, Class<?>> idClassMapping = new HashMap<>();
        idClassMapping.put("order",Order.class);
        idClassMapping.put("user",User.class);

        DefaultJackson2JavaTypeMapper jackson2JavaTypeMapper = new DefaultJackson2JavaTypeMapper();
        jackson2JavaTypeMapper.setIdClassMapping(idClassMapping);

        System.out.println("在jackson2JsonMessageConverter转换器中指定映射配置");
        jackson2JsonMessageConverter.setJavaTypeMapper(jackson2JavaTypeMapper);
        adapter.setMessageConverter(jackson2JsonMessageConverter);

        //设置处理器的消费消息的默认方法
        adapter.setDefaultListenerMethod("onMessage");
        container.setMessageListener(adapter);

        return container;
    }*/
import com.greathiit.printserver.receiver.message.ResultMessage;
import com.rabbitmq.client.Channel;
@Component
public class RabbitReceiver {
	//{"student":"20110101","name":"zhaojf","img":["http://img9.it.greathiit.com/public/bg.jpg"]}
	@RabbitListener(queues = { "print_result_queue" })
	@RabbitHandler
	public void onMessage(Message message, Channel channel) throws Exception {
		String json = new String(message.getBody());
		if(!StringUtils.isEmpty(json)) {
			Gson gson = new Gson();
			ResultMessage resultMessage = gson.fromJson(json, ResultMessage.class);
			PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
			DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
			// 设置打印参数
			PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
			aset.add(new Copies(1)); // 份数
			for(String img:resultMessage.getImg()) {
				URLConnection connection = new URL(img).openConnection();
				InputStream inputStream = connection.getInputStream();
				Doc doc = new SimpleDoc(inputStream, flavor, null);
				DocPrintJob job = printService.createPrintJob(); // 创建打印作业
				job.print(doc, aset);
			}
			channel.basicQos(1);
			Long deliveryTag = message.getMessageProperties().getDeliveryTag();
			channel.basicAck(deliveryTag, false);
		}
		
	}

}
